<?php
class Back_Model_Competitions extends Jnd_Model_Abstract
{
    protected $id = 0;
    protected $nom;
    protected $enfantDe = 0;
    protected $niveau;
    protected $url;

    /**
     *
     * @see Jnd_Model_Abstract::getId()
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     *
     * @see Jnd_Model_Abstract::setId()
     */
    public function setId( $id )
    {
        $this->id = $id;
    }

    public static function competitionsParNiveau( $enfantDe = 0 )
    {
        $mapper = new Back_Model_Mapper_Competitions();
        $donnees = $mapper->competitionsParNiveau($enfantDe);
        
        $competitions = array();
        foreach ($donnees as $donnee) {
            $competitions[] = new self($donnee['id']);
        }
        
        return $competitions;
    }

    public function listerPourSelect( $blank = true, $value = "nom", $cle = "id" )
    {
        $liste = self::competitionsParNiveau(0);
        $result = array();
        if ($blank) {
            $result[0] =  __("Racine");
        }
        $result = $this->_listerPourSelect($liste, $result, $value, $cle);
        return $result;
    }
    
    private function _listerPourSelect( $liste, & $result, $value = "nom", $cle = "id" ) {
        foreach ($liste as $competition) {
            $methodeCle = "get" . ucfirst($cle);
            $methodeValue = "get" . ucfirst($value);
            $result[$competition->$methodeCle()] = str_repeat('--', $competition->getNiveau()) . $competition->$methodeValue();
            $tmp = self::competitionsParNiveau($competition->getId());
            if (!empty($tmp)) {
                $result = $this->_listerPourSelect($tmp, $result, $value, $cle);
            }
        }
        return $result;
    }
}